package com.century.sjt.fragment;

import android.app.Activity;
import android.app.Fragment;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.century.sjt.R;
import com.century.sjt.util.Base64Utils;
import com.century.sjt.util.Constant;
import com.century.sjt.util.TipUtil;
import com.century.sjt.widget.gridpasswordview.GridPasswordView;

/**
 * @author:周玲波
 * @Description: 新密码
 * @date : 2016/8/19 17:20
 */
public class Setting_newpwdPayFragment extends Fragment {
    private GridPasswordView new_payPwd;
    private String newPwd;
    public static SharedPreferences mainSharedPreferences;
    private String isCashPwdSet;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.sjt_modify_newpwd_fragment,null);
        new_payPwd=(GridPasswordView)view.findViewById(R.id.paypwd_new);
        mainSharedPreferences =getActivity().getSharedPreferences(Constant.TAG, Activity.MODE_PRIVATE);
        isCashPwdSet = mainSharedPreferences.getString("isCashPwdSet", "");
        initEvent();
        return view;

    }

    public interface newClickListener{
        void onNewClick( String newPwd,int index);
    }

    private void initEvent(){
        new_payPwd.setOnPasswordChangedListener(new GridPasswordView.OnPasswordChangedListener() {
            @Override
            public void onChanged(String psw) {

            }

            @Override
            public void onMaxLength(String psw) {
                if (!(psw.length()==6)) {
                    TipUtil.showToast(getResources().getString(R.string.error_is_no6_old_password), getActivity(), 1);
                }else{
                    byte[] encodePwd = new_payPwd.getPassWord().getBytes();
                    newPwd = Base64Utils.encode(encodePwd);
                    if(getActivity() instanceof newClickListener){
                        if (!(isCashPwdSet.equals("1"))){
                            ((newClickListener) getActivity()).onNewClick(newPwd, 0);
                        }else {
                            ((newClickListener) getActivity()).onNewClick(newPwd, 3);
                        }
                    }
                }

            }
        });

    }


}
